package com.thesis.manage.service.impl;

import com.thesis.manage.common.service.impl.BaseServiceImpl;
import com.thesis.manage.domain.Classes;
import com.thesis.manage.mapper.ClassesMapper;
import com.thesis.manage.service.ClassesService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author php
 * @date 2018/1/18
 */
@Service
public class ClassesServiceImpl extends BaseServiceImpl<Classes> implements ClassesService {

    @Resource
    private ClassesMapper classesMapper;

    @Override
    protected Mapper<Classes> getMapper() {
        return this.classesMapper;
    }

    @Override
    protected Class<Classes> getEntityClass() {
        return Classes.class;
    }

    @Override
    public void importData(String classesInfo, Long majorId) {
        if (StringUtils.isNotBlank(classesInfo)) {
            String[] classesArray = classesInfo.split(",");
            Classes info;
            for (String classes : classesArray) {
                String[] split = classes.split("\\.");
                if (split.length == 2) {
                    info = new Classes();
                    info.setMajorId(majorId);
                    info.setName(split[0] + "级" + split[1] + "班");
                    info.setYear(Integer.parseInt(split[0]));
                    info.setSerialNumber(Integer.parseInt(split[1]));
                    if (this.isNotExist(majorId, info.getName())) {
                        this.save(info);
                    }
                }
            }
        }
    }

    @Override
    public List<Classes> findByMajorId(Long majorId) {
        Example example = new Example(Classes.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("majorId", majorId);
        example.orderBy("year").asc().orderBy("serialNumber").asc();
        return this.classesMapper.selectByExample(example);
    }

    @Override
    public List<String> findClassNamesByMajorId(Long majorId) {
        List<Classes> classes = this.findByMajorId(majorId);
        final List<String> result = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(classes)) {
            classes.forEach(var -> result.add(var.getName()));
        }
        return result;
    }

    @Override
    public Classes findByName(String classesName, Long majorId) {
        Classes classes = new Classes();
        classes.setName(classesName);
        classes.setMajorId(majorId);
        return this.findByOne(classes);
    }

    /**
     * 验证是否存在
     *
     * @param majorId 专业id
     * @param name    班级名称
     * @return booleam
     */
    private boolean isNotExist(Long majorId, String name) {
        Classes classes = new Classes();
        classes.setName(name);
        classes.setMajorId(majorId);
        return this.classesMapper.selectOne(classes) == null;
    }

}
